package scalab.core.component

import scalab.core.dependencies.Dependency

trait Scanner extends Filter{

	val in = mandatoryIn

	val recursive: Boolean
	
	def execute: Boolean = {

		var unscanned: Iterable[Dependency] = in.sources
		while(!unscanned.isEmpty){
			val found = unscanned flatMap {u => scan(u)}
			unscanned = if(recursive) found else Nil
		}
		returnValue
	}

	def scan(d: Dependency): Iterable[Dependency]

	//is the iterable of the set always ordered the same way
	def instanceId: String = ""
	def returnValue: Boolean = true
}

object Scanner{

	import java.io.File

	def apply(componentName: String, recurse: Boolean, scanner: (Dependency => Iterable[Dependency])): Scanner = new Scanner{
		override val recursive: Boolean = recurse
		override def scan(d: Dependency): Iterable[Dependency] = scanner(d)
		override val name: String = componentName
	}

	def apply(recurse: Boolean, scanner: (Dependency => Iterable[Dependency])): Scanner = apply("Scanner", recurse, scanner)
	def apply(componentName: String, scanner: (Dependency => Iterable[Dependency])): Scanner = apply(componentName, true, scanner)
	def apply(scanner: (Dependency => Iterable[Dependency])): Scanner = apply(true, scanner)
}
